<?php
/* 
 * class.discountlist.php
 * 我的城市首页-更多优惠
 * @author samuel
 * @date 2009-05-23
 */
include_once 'class.mycityBase.php';
class mycity_discountlist extends mycityBase {

    function  __construct($core) {
        parent::__construct();

        //参数处理
	    $this->page = $this->params['page'] ? (int)$this->params['page'] : 1;
	    $this->user_id = $this->params['uid'] ? (int)$this->params['uid'] : 0;
	    $this->type = $this->params['type'] ? strtolower(trim($this->params['type'])) : 'all';
	    if(!$this->user_id){
		    $this->error = "用户ID不合法！";
		    $this->jumpError();
		    exit;
	    }
        $this->run();
    }

    function run(){
    	//WAP页面
	    //header("Content-type: text/vnd.wap.wml");
	    //TITLE
	    $page_title = "我的城市-优惠信息";
	    $content = "";
	    $content .= "<a href='".$_SERVER['PHP_SELF']."?type=".$this->type."&amp;uid=".$this->user_id."' alt='返回上级'>返回上级</a><br/>";
	    $content .= "<a href='".$_SERVER['PHP_SELF']."?act=discountlist&amp;uid=".$this->user_id."&amp;type=all' alt='优惠'>优惠</a>|<a href='".$_SERVER['PHP_SELF']."?act=discountlist&amp;uid=".$this->user_id."&amp;type=favor' alt='收藏'>收藏</a>|<a href='".$_SERVER['PHP_SELF']."?act=discountlist&amp;uid=".$this->user_id."&amp;type=latest' alt='最新'>最新</a><br/>";
	    
	    //优惠信息（10条，按时间排序）
	    $sellerBase = $this->core->load('seller::sellerBase');
	    $pagesize = 10;
	    $displaypage = 5;
	    $page = $this->page;
	    $current_url = $_SERVER['PHP_SELF']."?act=discountlist&amp;uid=".$this->user_id."&amp;type=".$this->type;
	    if($this->type=='all' || $this->type=='latest'){
	        $sql = "SELECT count(*) FROM `fun_seller_discount` WHERE 1 AND `SellerMsgIs`='1'";
	        $counts = $this->core->db->get_var($sql);
	        $totalPages = ceil($counts/$pagesize);
	        $pager = $this->core->utility->wap_pager($totalPages, $page, $displaypage, $current_url);
	        $arr_filter = array();
	        $arr_filter[] = array('attr'=>'SellerMsgIs', 'value'=>'1');
	        $arr_limit = array('page'=>$page, 'pagesize'=>$pagesize, 'url'=>'');
	        $arr_info = $sellerBase->getSellerDiscountList($arr_filter, array("SellerMsgTime"=>"desc"), $arr_limit);
	        
	    }elseif($this->type == 'favor'){
	        $favorSellers = $this->core->load('myhome::myhomeBase')->getUserFavorites($this->user_id);
	        $arr_favors = array();
	        if($favorSellers){
	            foreach($favorSellers as $favor){
	                $arr_favors[] = $favor['SellerID'];
	            }
	        }
	        $str_favors = @implode(',', $arr_favors);
	        $sql = "SELECT count(*) FROM `fun_seller_discount` WHERE 1 AND `SellerMsgIs`='1' AND `SellerID` IN (".$str_favors.")";
	        $counts = $this->core->db->get_var($sql);
	        $totalPages = ceil($counts/$pagesize);
	        $pager = $this->core->utility->wap_pager($totalPages, $page, $displaypage, $current_url);
	        
	        $offset = ($page - 1) * $pagesize;
	        $sql = "SELECT a.`SellerDisID`, a.`SellerMsg`, a.`SellerMsgIs`, a.`SellerMsgTime`, b.`SellerNameCN` FROM `fun_seller_discount` as a LEFT JOIN `fun_seller` as b ON a.`SellerID`=b.`SellerID` WHERE 1 AND a.`SellerMsgIs` = '1' AND a.`SellerID` IN (".$str_favors.") ORDER BY a.`SellerMsgTime` desc LIMIT $offset,$pagesize";
	        $discs = $this->core->db->get_results($sql);
	        if(!empty($discs)){
	            $arr_sellerDiscs = array();
                foreach($discs as $disc){
                    $arr_sellerDiscs[] = array(
                        'SellerDisID' => $disc->SellerDisID,
                        'SellerMsg' => $disc->SellerMsg,
                        'SellerMsgIs' => $disc->SellerMsgIs,
					    'SellerNameCN' => $disc->SellerNameCN,
					    'SellerMsgTime' => $disc->SellerMsgTime
                    );
                }
            }
            $arr_info['list'] = $arr_sellerDiscs;
	    }
	    
	    $content .= $pager."<br/>";
	    if(is_array($arr_info['list'])){
	        foreach($arr_info['list'] as $info){
		    $content .= "<img src='' alt='优惠图标' /><a href='".$_SERVER['PHP_SELF']."?act=discountshow&amp;type=".$this->type."&amp;uid=".$this->user_id."&amp;id=".$info['SellerDisID']."' alt=''>".$info['SellerMsg']."</a><br/>";
	        }
	    }
	    $content .= $pager."<br/>";
	
	    //商家顶级分类
	    $arr_filter = array();
	    $arr_filter[] = array('attr'=>'SellerClassOwner', 'value'=>'0');
	    $arr_limit = array('page'=>1, 'pagesize'=>3, 'url'=>'');
	    $arr_info = $sellerBase->getInfoList(array('table'=>'fun_seller_class', 'primary'=>'SellerClassID'), "", $arr_filter, array("SellerClassID"=>"desc"), $arr_limit);
	    if(is_array($arr_info['list'])){
	        $arr_tmp = array();
	        foreach($arr_info['list'] as $info){
		    $arr_tmp[] = "<a href='./cate.php?id=".$info['SellerClassID']."' alt=''>".$info['SellerClassName']."</a>";
	        }
	        $str_tmp = @implode('|', $arr_tmp);
	        $content .= $str_tmp."<a href='' alt='更多'>>></a><br/>";
	    }
	    //商家地区
	    $arr_limit = array('page'=>1, 'pagesize'=>3, 'url'=>'');
	    $arr_info = $sellerBase->getInfoList(array('table'=>'fun_seller_area', 'primary'=>'SellerAreaID'), "", "", array("SellerAreaID"=>"desc"), $arr_limit);
	    if(is_array($arr_info['list'])){
	        $arr_tmp = array();
	        foreach($arr_info['list'] as $info){
		    $arr_tmp[] = "<a href='./cate.php?id=".$info['SellerAreaID']."' alt=''>".$info['SellerAreaNameCN']."</a>";
	        }
	        $str_tmp = @implode('|', $arr_tmp);
	        $content .= $str_tmp."<a href='' alt='更多'>>></a><br/>";
	    }
	    //商家商圈
	    $arr_limit = array('page'=>1, 'pagesize'=>3, 'url'=>'');
	    $arr_info = $sellerBase->getInfoList(array('table'=>'fun_seller_cbd', 'primary'=>'SellerCBDID'), "", "", array("SellerCBDID"=>"desc"), $arr_limit);
	    if(is_array($arr_info['list'])){
	        $arr_tmp = array();
	        foreach($arr_info['list'] as $info){
		    $arr_tmp[] = "<a href='./cate.php?id=".$info['SellerCBDID']."' alt=''>".$info['SellerCBDNameCN']."</a>";
	        }
	        $str_tmp = @implode('|', $arr_tmp);
	        $content .= $str_tmp."<a href='' alt='更多'>>></a><br/>";
	    }
	    //商家分类
	    $arr_filter = array();
	    $arr_filter[] = array('attr'=>'SellerClassOwner', 'value'=>'1');
	    $arr_limit = array('page'=>1, 'pagesize'=>3, 'url'=>'');
	    $arr_info = $sellerBase->getInfoList(array('table'=>'fun_seller_class', 'primary'=>'SellerClassID'), "", $arr_filter, array("SellerClassID"=>"desc"), $arr_limit);
	    if(is_array($arr_info['list'])){
	        $arr_tmp = array();
	        foreach($arr_info['list'] as $info){
		    $arr_tmp[] = "<a href='./cate.php?id=".$info['SellerClassID']."' alt=''>".$info['SellerClassName']."</a>";
	        }
	        $str_tmp = @implode('|', $arr_tmp);
	        $content .= $str_tmp."<a href='' alt='更多'>>></a><br/>";
	    }
	    //搜索
	    $content .= "<input type='text' name='searchkeyword' value='' maxlength='20' size='15' />";
	    $content .= "<anchor title=\"搜索\">搜<go href=\"./search.php\" method=\"post\">
		        <postfield name=\"type\" value=\"city\" />
		        <postfield name=\"searchkeyword\" value=\"$(searchkeyword)\" />
		        </go></anchor><br/><br/><br/>";
	
	    $content .= "<a href='' alt='返回上级'>返回上级</a><br/>";
	    $content .= "<a href='' alt='建议帮助'>建议帮助</a>";



	    $this->core->tpl->assign('content', $content);
	    $this->core->tpl->assign('page_title', $page_title);
	    $this->core->tpl->display('comm.wml');
    
    }
}


?>
